/*
 * @Description: 全宗reducer
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2022-06-01 11:31:32
 */

import FileConstants from '../constants';

export const FilesReducer = (state, action) => {
  switch (action.type) {
    case FileConstants.ADD_FILE:
      return [
        ...state,
        {
          title: action.book.title,
          author: action.book.author
        }
      ];

    case FileConstants.DELETE_FILE:
      return state.filter((book) => book.id !== action.id);

    case FileConstants.SHOW_DIALOG:
      return { ...state, dialog: { visible: true } };

    case FileConstants.HIDE_DIALOG:
      return { ...state, dialog: { visible: false } };

    case FileConstants.LOADING: {
      return { ...state, loading: true };
    }

    case FileConstants.DISPLAY_ITEMS: {
      console.log('display', action);
      const { listArr } = state;
      listArr[parseInt(action.level, 10)] = {
        list: action.data.data
      };
      return {
        ...state,
        listArr,
        metadataList: action.data.metadataList
      };
    }
    default:
      return state;
  }
};
